import React from 'react';
import { Button, Icon, message, Modal } from 'antd';
import { WeaTools, WeaLocaleProvider, WeaTop, WeaDialog, WeaFormItem, WeaInput, WeaUpload } from 'ecCom';
import { WeaForm } from 'comsMobx';
import Message from './Message';
import './WeaLicenseSubmit.less';

const getLabel = WeaLocaleProvider.getLabel;
const confirm = Modal.confirm;

class WeaLicenseSubmitCluster extends React.Component {
  constructor(props) {
    super(props);
    this.editForm = new WeaForm();
    this.state = { loading: false, licenseCode: '', ip: '', data: {}, addr: '', method: '', messageDesc: '', isUpload: false, uploadData: [] };
  }

  componentWillMount() {
    const { visible, addr } = this.props;
    if (visible) {
      this.onLoad(addr);
    }
  }

  componentWillReceiveProps(nextProps) {
    const { visible, addr } = nextProps;
    if (visible) {
      this.onLoad(addr);
    }
  }

  render() {
    const { location = {}, visible } = this.props;
    const { query = {} } = location;
    const { display } = query;
    const { loading, licenseCode, ip, data, method, addr, messageDesc, uploadData } = this.state;
    const { code = '', code_old = '', code_new = '', code_rep = '' } = data;
    const colProps = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
    let Content = (
      <div className="wea-license-form-group">
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@6tzk96`} label={getLabel(22910, '验证码')} {...colProps}>
          <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@z4hihk`} style={{ width: '270px' }} type="password" value={code} viewAttr={3} onChange={value => this.setState({ data: { ...data, code: value } })} />
          <span className="wea-license-form-change" onClick={() => this.setState({ isChangeCode: true })}>
            {getLabel(517208, '更改验证码')}
          </span>
        </WeaFormItem>
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@nfg4x4`} label={getLabel(132196, '服务器IP')} {...colProps}>
          <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@ul0ndp`} value={ip} viewAttr={1} />
        </WeaFormItem>
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@xythkz`} label={`license${getLabel(18493, '文件')}`} {...colProps}>
          <WeaUpload ecId={`${this && this.props && this.props.ecId || ''}_WeaUpload@sznpaj`}
            ref="WeaUpload"
            datas={uploadData}
            uploadId="LicenseSubmit"
            uploadUrl="/api/system/license/ln/LNServlet"
            uploadParams={{ code, method, addr }}
            category="string"
            clearWhenReset={false}
            multiSelection={false}
            maxFilesNumber={1}
            showClearAll={false}
            autoUpload={false}
            limitType="license"
            viewAttr={3}
            onChange={(ids, list, args) => this.afterUpload(args)}
            getShowListDatas={(ids, list) => this.isUpload(list)}
          />
        </WeaFormItem>
        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@bozc6x`} label={getLabel(15019, '识别码')} {...colProps} underline>
          <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@xiy2v5`} value={licenseCode} viewAttr={1} />
        </WeaFormItem>
        <div className="wea-license-tip-msg">
          <Icon ecId={`${this && this.props && this.props.ecId || ''}_Icon@16bejn`} type="exclamation-circle" />
          <span style={{ marginLeft: '10px' }}>{getLabel(517211, '提示：公司名称及识别码提交给软件供应商，以获取License')}</span>
        </div>
      </div>
    );

    if (this.state.isChangeCode) {
      Content = (
        <div className="wea-license-form-group">
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@9pl9za`} label={getLabel(132196, '服务器IP')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@y3orow`} value={ip} viewAttr={1} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@0x0k6p`} label={getLabel(517209, '旧验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@db3jkl`} type="password" value={code_old} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_old: value } })} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@exx68e`} label={getLabel(22910, '验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@7mwx7q`} type="password" value={code_new} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_new: value } })} />
          </WeaFormItem>
          <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1ejk4q`} label={getLabel(517210, '确认验证码')} {...colProps}>
            <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@cse9v6`} type="password" value={code_rep} viewAttr={3} onChange={value => this.setState({ data: { ...data, code_rep: value } })} />
          </WeaFormItem>
        </div>
      );
    }

    if (this.state.isShowMsg) {
      Content = <Message ecId={`${this && this.props && this.props.ecId || ''}_Message@r6qjyh`} message={this.state.message} />;
    }

    if (display == 'page') {
      return (
        <WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@tgs1ok`} loading={loading} title={`License${getLabel(615, '提交')}`} icon={<i className="icon-portal-license" />} iconBgcolor="#a7adb5" buttons={this.getButtons()}>
          {Content}
        </WeaTop>
      );
    }
    return (
      <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@fdx1nj`}
        visible={visible}
        loading={loading}
        title={messageDesc ? getLabel(28535, '信息提示') : `License${getLabel(615, '提交')}`}
        icon="icon-portal-license"
        iconBgcolor="#a7adb5"
        style={{ width: 600 }}
        zIndex={999}
        hasScroll={true}
        buttons={this.getButtons()}
        onCancel={this.onCancel}
      >
        {Content}
      </WeaDialog>
    );
  }
  getButtons = () => {
    const { isShowMsg, message, isChangeCode, messageDesc, method, addr, fileid, data } = this.state;
    const buttons = [];
    if (messageDesc) {
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rx5zfc`} type="primary" onClick={() => this.reUpload({ fileid, addr, code: data.code, method })}>
          {getLabel(826, '确定')}
        </Button>,
      );
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@enfh6c`} type="primary" onClick={this.onBack}>
          {getLabel(31129, '取消')}
        </Button>,
      );
    } else if (isChangeCode) {
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@wic680`} type="primary" onClick={this.onCodeSubmit}>
          {getLabel(615, '提交')}
        </Button>,
      );
      buttons.push(
        <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@uei0dr`} type="primary" onClick={this.onBack}>
          {getLabel(31129, '取消')}
        </Button>,
      );
    } else if (isShowMsg) {
        if (message == '1') {
          buttons.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ml91in`} type="primary" onClick={this.onCancel}>
              {this.props.from == 'login' ? getLabel(674, '登录') : getLabel(309, '关闭')}
            </Button>,
          );
        } else {
          buttons.push(
            <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@waxmpw`} type="primary" onClick={this.onBack}>
              {getLabel(1290, '返回')}
            </Button>,
          );
        }
      } else {
        buttons.push(
          <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@cw5cvj`} type="primary" onClick={this.onSubmit}>
            {getLabel(615, '提交')}
          </Button>,
        );
      }
    return buttons;
  };

  onSubmit = () => {
    const { data, isUpload } = this.state;
    const { code = '' } = data;
    if (code.trim() != '' && isUpload) {
      this.refs.WeaUpload.doUpload();
    } else {
      message.warning(getLabel(15859, '必要信息不完整！'));
    }
  };

  onCodeSubmit = () => {
    const { data, addr } = this.state;
    const { code_old = '', code_new = '', code_rep = '' } = data;

    if (code_old.trim() != '' && code_new.trim() != '' && code_rep.trim() != '') {
      if (code_new === code_rep) {
        this.setState({ loading: true });
        WeaTools.callApi('/api/system/license/ln/LNServlet', 'POST', { method: 'modifyCode', addr, passwordold: code_old, passwordnew: code_new, confirmpassword: code_rep }).then((result) => {
          const msg = result.message;
          if (msg == '1') {
            message.error(getLabel(128878, '验证码有误'));
          } else {
            message.success(msg);
            this.onBack();
          }
          this.setState({ loading: false });
        });
      } else {
        message.warning(getLabel(128878, '验证码有误'));
      }
    } else {
      message.warning(getLabel(384134, '必要信息不完整！'));
    }
  };

  onBack = () => {
    this.setState({ data: {}, isShowMsg: false, isChangeCode: false, messageDesc: '', method: 'submitLicense', uploadData: [] });
  };

  onCancel = () => {
    this.props.onHide();
    this.setState({ data: {}, isShowMsg: false, isChangeCode: false, messageDesc: '', isUpload: false, method: 'submitLicense' });
  };

  onLoad = (parames) => {
    this.setState({ loading: true });
    WeaTools.callApi('/api/system/license/InLicense', 'POST', parames).then((result) => {
      this.setState({ loading: false, licenseCode: result.licenseCode, ip: result.ip, addr: result.addr, method: result.method });
    });
  };

  afterUpload = (parames) => {
    if (parames[0]) {
      if (parames[0].messageDesc) {
        this.setState({ uploadData: parames, isShowMsg: false, message: parames[0].messageDesc, messageDesc: parames[0].messageDesc, method: 'reUpdateLicense', fileid: parames[0].fileid });
        const { method, addr, fileid, data } = this.state;
        confirm({
          title: getLabel(28535, '信息提示'),
          content: this.state.messageDesc,
          onOk: () => this.reUpload({ fileid, addr, code: data.code, method }),
          onCancel: () => {
            this.onBack();
          },
        });
      } else {
        this.setState({ isShowMsg: true, message: parames[0].message });
      }
    }
  }

  reUpload = (parames) => {
    WeaTools.callApi('/api/system/license/ln/LNServlet', 'POST', parames).then((result) => {
      this.setState({ isShowMsg: true, loading: false, message: result.message, messageDesc: '', uploadData: [] });
    });
  };

  isUpload = (list) => {
    this.setState({ isUpload: list.length > 0 });
  };
}

export default WeaLicenseSubmitCluster;
